package com.example.wangzh.mytest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.wangzh.mytest.constant.LogTag;

public class MatrixTransformView extends View {

    public MatrixTransformView(Context context) {
        super(context);
        init();
    }

    public MatrixTransformView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MatrixTransformView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MatrixTransformView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private Matrix matrix = new Matrix();
    private float[] originalPoints = {
            0, 0,    // 左上角
            100, 100 // 右下角
    };
    // 放大后的中心点坐标
    float scaledCenterX = 100; // 原始宽度 100 * 2 / 2 = 100
    float scaledCenterY = 100;
    private float[] transformedPoints = new float[4];

    private void init() {
        setBackgroundColor(Color.LTGRAY);
        // 先放大
        matrix.postScale(2f, 2f, 0, 0);
        // 再绕放大后的中心旋转
        matrix.postRotate(45, scaledCenterX, scaledCenterY);
        // 应用变换到坐标点
        matrix.mapPoints(transformedPoints, originalPoints);

        // 打印变换后的坐标
        Log.d(LogTag.WZH_TEST, "原始坐标 (0,0) → 变换后: (" + transformedPoints[0] + ", " + transformedPoints[1] + ")");
        Log.d(LogTag.WZH_TEST, "原始坐标 (100,100) → 变换后: (" + transformedPoints[2] + ", " + transformedPoints[3] + ")");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.concat(matrix); // 应用矩阵到 Canvas
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawRect(0, 0, 100, 100, paint); // 绘制原始矩形
        canvas.restore();
    }
}